package com.kh.pajx.sop.view.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.kh.pajx.sop.base.ModelDrivenBaseAction;
import com.kh.pajx.sop.domain.SysPosDebug;
import com.kh.pajx.sop.util.common.DwzAjaxResult;
import com.kh.pajx.sop.util.sop.PropertyValidate;
import com.opensymphony.xwork2.ActionContext;

@Controller
@Scope("prototype")
public class SysSopDebugAction extends ModelDrivenBaseAction<SysPosDebug> {

    private HttpServletRequest request;

    public String listUI() {
	DwzAjaxResult dwz = new DwzAjaxResult();
	request = ServletActionContext.getRequest();
	String status = request.getParameter("status");
	try {
	    if (!"1".equals(status) && !"2".equals(status)) {
		throw new Exception("参数被篡改");
	    }
	} catch (Exception e) {
	    dwz.setStatusCode("300");
	    dwz.setConfirmMsg("参数被篡改" + e.getMessage());
	    DwzAjaxResult.josnMessage(dwz);
	    e.printStackTrace();
	    return null;
	}
	request.setAttribute("status", status);
	if ("2".equals(status)) {
	    return "teaGetUI";
	}
	return "getUI";
    }

    // 调试信息
    public String list() {
	DwzAjaxResult dar = new DwzAjaxResult();
	Map<String, String> params = new HashMap<String, String>();
	request = ServletActionContext.getRequest();
	String sclId = request.getParameter("model.sclId");
	
	String status = request.getParameter("status");
	String cardSn = request.getParameter("pos_cardSn");
	String startDate = request.getParameter("pos_startDate");
	String endDate = request.getParameter("pos_endDate");
	String stuName = request.getParameter("debug_stuName");
	String stuNo = request.getParameter("debug_stuNo");
	request.setAttribute("sclId", sclId);
	request.setAttribute("cardSn", cardSn);
	request.setAttribute("startDate", startDate);
	request.setAttribute("endDate", endDate);
	request.setAttribute("stuName", stuName);
	request.setAttribute("stuNo", stuNo);
	params.put("sclId", sclId);
	params.put("cardSn", cardSn);
	params.put("startDate", startDate);
	params.put("endDate", endDate);
	params.put("stuName", stuName);
	params.put("stuNo", stuNo);
	try {
	    if (!"1".equals(status) && !"2".equals(status)) {
		throw new Exception("参数被篡改");
	    }
	    if(StringUtils.isBlank(sclId)){
		    throw new Exception("学校不能为空");
	    }else{
		if(!PropertyValidate.validateUUID(sclId)){
		    throw new Exception("输入的学校编号不合法！");
		}
	    }
	    if(StringUtils.isNotBlank(cardSn)&&!PropertyValidate.validateStrByLength(cardSn, 30)){
		throw new Exception("射频卡输入不合法");
	    }
	    if(StringUtils.isNotBlank(startDate)&&!PropertyValidate.validateStrByLength(startDate, 10)){
		throw new Exception("起始日期输入不合法");
	    }
	    if(StringUtils.isNotBlank(endDate)&&!PropertyValidate.validateStrByLength(endDate, 10)){
		throw new Exception("结束日期不合法");
	    }
	    if(StringUtils.isNotBlank(stuName)&&!PropertyValidate.validateStrByLength(stuName, 30)){
	        throw new Exception("姓名输入不合法");
	    }
	    if(StringUtils.isNotBlank(stuNo)&&!PropertyValidate.validateStrByLength(stuNo, 12)){
	        throw new Exception("卡号输入不合法");
	    }
	} catch (Exception e) {
	    dar.setStatusCode("300");
	    dar.setMessage("查询失败！" + e.getMessage());
	    DwzAjaxResult.josnMessage(dar);
	}
	if ("1".equals(status)) {
	    try {
		String graId = request.getParameter("pos_graId");
		if(StringUtils.isNotBlank(graId)&&!PropertyValidate.validateUUID(graId)){
		    throw new Exception("年级输入不合法");
		}
		String clsId = request.getParameter("pos_clsId");
		if(StringUtils.isNotBlank(clsId)&&!PropertyValidate.validateUUID(clsId)){
		    throw new Exception("班级输入不合法");
		}
		request.setAttribute("graId", graId);
		request.setAttribute("clsId", clsId);
		params.put("graId", graId);
		params.put("clsId", clsId);
		int count = sysSopDebugService.debugCount(params);
		List<List<String>> mess = sysSopDebugService.getDebugMes(params, numPerPage, pageNum);
		request.setAttribute("mess", mess);
		Map<String, List<String>> gras = sysSopDebugService.getAllGra(sclId);
		request.setAttribute("grades", gras);
		Map<String, List<String>> cls = sysSopDebugService.getAllCLS(sclId);
		request.setAttribute("cls", cls);
		Map<String, List<String>> dic = sysSopDebugService.getAllDic();
		request.setAttribute("dic", dic);
		ActionContext.getContext().getValueStack().push(new com.kh.pajx.sop.domain.PageBean(pageNum, numPerPage, count, mess));
	    } catch (Exception e) {
		dar.setStatusCode("300");
		dar.setMessage("查询失败！"+e.getMessage());
		DwzAjaxResult.josnMessage(dar);
		e.printStackTrace();
	    }
	    return "list";
	} else {
	    try {
		int count = sysSopDebugService.teaDebugCount(params);
		Map<String, List<String>> dic = sysSopDebugService.getAllDic();
		// System.out.println("------------"+dic.size());
		request.setAttribute("dic", dic);
		List<List<String>> mess = sysSopDebugService.getTeaDebugMes(params, numPerPage, pageNum);
		ActionContext.getContext().getValueStack().push(new com.kh.pajx.sop.domain.PageBean(pageNum, numPerPage, count, mess));
	    } catch (Exception e) {
		dar.setStatusCode("300");
		dar.setMessage("查询失败！"+e.getMessage());
		DwzAjaxResult.josnMessage(dar);
		e.printStackTrace();
	    }
	    return "teaList";
	}
    }

    // 刷卡记录过滤条件界面
    public String recordUI() {
	DwzAjaxResult dwz = new DwzAjaxResult();
	request = ServletActionContext.getRequest();
	String status = request.getParameter("status");
	request.setAttribute("status", status);
	try {
	    if (!"1".equals(status) && !"2".equals(status)) {
		throw new Exception("参数被篡改");
	    }
	} catch (Exception e) {
	    dwz.setStatusCode("300");
	    dwz.setConfirmMsg("参数被篡改" + e.getMessage());
	    DwzAjaxResult.josnMessage(dwz);
	    e.printStackTrace();
	    return null;
	}
	if ("1".equals(status)) {
	    return "recordUI";
	} else {
	    return "teaRecordUI";
	}

    }

    public String listRecord() {
	DwzAjaxResult dar = new DwzAjaxResult();
	request = ServletActionContext.getRequest();
	Map<String, String> params = new HashMap<String, String>();
	String sclId = request.getParameter("model.sclId");
	String startDate = request.getParameter("record_startDate");
	String endDate = request.getParameter("record_endDate");
	String equNo = request.getParameter("record_equNo");
	String stuName = request.getParameter("record_stuName");
	String stuNo = request.getParameter("record_stuNo");
	request.setAttribute("startDate", startDate);
	request.setAttribute("endDate", endDate);
	request.setAttribute("equNo", equNo);
	request.setAttribute("stuName", stuName);
	request.setAttribute("stuNo", stuNo);
	request.setAttribute("sclId", sclId);
	params.put("stuName", stuName);
	params.put("startDate", startDate);
	params.put("endDate", endDate);
	params.put("equNo", equNo);
	params.put("stuNo", stuNo);
	params.put("sclId", sclId);
	String status = request.getParameter("status");
	try {
	    if (!"1".equals(status) && !"2".equals(status)) {
		throw new Exception("参数被篡改");
	    }
	    if(StringUtils.isBlank(sclId)){
		    throw new Exception("学校不能为空");
	    }else{
		if(!PropertyValidate.validateUUID(sclId)){
		    throw new Exception("输入的学校编号不合法！");
		}
	    }
	    if(StringUtils.isNotBlank(equNo)&&!PropertyValidate.validateStrByLength(equNo, 30)){
		throw new Exception("设备编号输入不合法");
	    }
	    if(StringUtils.isNotBlank(startDate)&&!PropertyValidate.validateStrByLength(startDate, 10)){
		throw new Exception("起始日期输入不合法");
	    }
	    if(StringUtils.isNotBlank(endDate)&&!PropertyValidate.validateStrByLength(endDate, 10)){
		throw new Exception("结束日期不合法");
	    }
	    if(StringUtils.isNotBlank(stuName)&&!PropertyValidate.validateStrByLength(stuName, 30)){
	        throw new Exception("姓名输入不合法");
	    }
	    if(StringUtils.isNotBlank(stuNo)&&!PropertyValidate.validateStrByLength(stuNo, 12)){
	        throw new Exception("卡号输入不合法");
	    }
	} catch (Exception e) {
	    dar.setStatusCode("300");
	    dar.setConfirmMsg("参数被篡改" + e.getMessage());
	    DwzAjaxResult.josnMessage(dar);
	    e.printStackTrace();
	    return null;
	}
	if ("1".equals(status)) {
	    try {
		String graId = request.getParameter("pos_graId");
		if(StringUtils.isNotBlank(graId)&&!PropertyValidate.validateUUID(graId)){
		    throw new Exception("年级输入不合法");
		}
		String clsId = request.getParameter("pos_clsId");
		if(StringUtils.isNotBlank(clsId)&&!PropertyValidate.validateUUID(clsId)){
		    throw new Exception("班级输入不合法");
		}
		request.setAttribute("clsId", clsId);
		request.setAttribute("graId", graId);
		params.put("graId", graId);
		params.put("clsId", clsId);
		int count = sysSopDebugService.recordCount(params);
		List<List<String>> records = sysSopDebugService.getAllRecord(params, numPerPage, pageNum);
		request.setAttribute("mess", records);
		Map<String, List<String>> gras = sysSopDebugService.getAllGra(sclId);
		request.setAttribute("grades", gras);
		Map<String, List<String>> cls = sysSopDebugService.getAllCLS(sclId);
		request.setAttribute("cls", cls);
		// Map<String,List<String>> dic =
		// sysSopDebugService.getAllDic();
		// request.setAttribute("dic", dic);
		ActionContext.getContext().getValueStack().push(new com.kh.pajx.sop.domain.PageBean(pageNum, numPerPage, count, records));
	    } catch (Exception e) {
		dar.setStatusCode("300");
		dar.setMessage("查询失败！");
		DwzAjaxResult.josnMessage(dar);
		e.printStackTrace();
	    }
	    return "record";
	} else {
	    // Map<String,List<String>> dic = sysSopDebugService.getAllDic();
	    // request.setAttribute("dic", dic);
	    int count = sysSopDebugService.teaRecordCount(params);
	    List<List<String>> records = sysSopDebugService.getTeaRecordMes(params, numPerPage, pageNum);
	    ActionContext.getContext().getValueStack().push(new com.kh.pajx.sop.domain.PageBean(pageNum, numPerPage, count, records));
	    return "teaRecord";
	}

    }

    // 短信记录过滤条件界面
    public String goCmppMessUI() {
	// System.out.println("进入");
	return "messUI";
    }

    public String cmppMessList() {
	DwzAjaxResult dwz = new DwzAjaxResult();
	Map<String, String> params = new HashMap<String, String>();
	request = ServletActionContext.getRequest();
	String sclId = request.getParameter("model.sclId");
	String phone = request.getParameter("cmpp_phone");
	String startDate = request.getParameter("cmpp_startDate");
	String endDate = request.getParameter("cmpp_endDate");
	String stuName = request.getParameter("cmpp_stuName");
	String stuNo = request.getParameter("cmpp_stuNo");
	String graId = request.getParameter("cmpp_graId");
	String clsId = request.getParameter("cmpp_clsId");
	request.setAttribute("sclId", sclId);
	request.setAttribute("phone", phone);
	request.setAttribute("startDate", startDate);
	request.setAttribute("endDate", endDate);
	request.setAttribute("stuName", stuName);
	request.setAttribute("stuNo", stuNo);
	params.put("sclId", sclId);
	params.put("phone", phone);
	params.put("startDate", startDate);
	params.put("endDate", endDate);
	params.put("stuName", stuName);
	params.put("stuNo", stuNo);
	request.setAttribute("graId", graId);
	request.setAttribute("clsId", clsId);
	params.put("graId", graId);
	params.put("clsId", clsId);
	try {
	    if(StringUtils.isBlank(sclId)){
		    throw new Exception("学校不能为空");
	    }else{
		if(!PropertyValidate.validateUUID(sclId)){
		    throw new Exception("输入的学校编号不合法！");
		}
	    }
	    if(StringUtils.isNotBlank(phone)&&!PropertyValidate.validatePhone(phone)){
		throw new Exception("手机号输入不合法");
	    }
	    if(StringUtils.isNotBlank(startDate)&&!PropertyValidate.validateStrByLength(startDate, 10)){
		throw new Exception("起始日期输入不合法");
	    }
	    if(StringUtils.isNotBlank(endDate)&&!PropertyValidate.validateStrByLength(endDate, 10)){
		throw new Exception("结束日期不合法");
	    }
	    if(StringUtils.isNotBlank(stuName)&&!PropertyValidate.validateStrByLength(stuName, 30)){
	        throw new Exception("姓名输入不合法");
	    }
	    if(StringUtils.isNotBlank(stuNo)&&!PropertyValidate.validateStrByLength(stuNo, 12)){
	        throw new Exception("卡号输入不合法");
	    }
	    Map<String, List<String>> gras = sysSopDebugService.getAllGra(sclId);
	    request.setAttribute("grades", gras);
	    Map<String, List<String>> cls = sysSopDebugService.getAllCLS(sclId);
	    request.setAttribute("cls", cls);
	    List<List<String>> mess = sysSopDebugService.getCmppMess(params, numPerPage, pageNum);
	    int count = sysSopDebugService.getCmppCounts(params);
	    ActionContext.getContext().getValueStack().push(new com.kh.pajx.sop.domain.PageBean(pageNum, numPerPage, count, mess));
	} catch (Exception e) {
	    dwz.setStatusCode("300");
	    dwz.setConfirmMsg("查询失败"+e.getMessage());
	    DwzAjaxResult.josnMessage(dwz);
	    e.printStackTrace();
	}

	return "cmppList";
    }

    // 亲情电话调试
    public String toTelSop() {
	return "toTelSop";
    }

    public String show_tel_record() {
	DwzAjaxResult dwz = new DwzAjaxResult();
	request = ServletActionContext.getRequest();
	String cardNo = request.getParameter("tel_cardNo");
	String cardSn = request.getParameter("tel_cardSn");
	request.setAttribute("cardNo", cardNo);
	request.setAttribute("cardSn", cardSn);
	try {
	    if(StringUtils.isBlank(cardSn)&&StringUtils.isBlank(cardNo)){
		throw new Exception("必须输入查询信息");
	    }
	    if(!PropertyValidate.validateStrByLength(cardNo, 12)){
		throw new Exception("卡号输入不合法");
	    }
	    if(!PropertyValidate.validateStrByLength(cardSn, 8)){
		throw new Exception("卡内码输入不合法");
	    }
	    List<String> list = sysSopDebugService.getTelRecord(cardSn, cardNo);
	    ActionContext.getContext().put("telList", list);
	} catch (Exception e) {
	    dwz.setStatusCode("300");
	    dwz.setConfirmMsg("查询错误"+e.getMessage());
	    DwzAjaxResult.josnMessage(dwz);
	    e.printStackTrace();
	}
	return "show_tel_record";
    }

}
